/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-09 17:24:08  
 * Copyright (c) 2015- joysuch-版权所有     
 */

package com.joysuch.wwyt.bp.controller;

import javax.validation.Valid;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.joysuch.wwyt.bp.bean.BpExamStandardEditBean;
import com.joysuch.wwyt.bp.bean.BpExamStandardListBean;
import com.joysuch.wwyt.bp.bean.BpExamStandardListBean.Search;
import com.joysuch.wwyt.bp.entity.BpExamStandard;
import com.joysuch.wwyt.bp.enums.TargetResponsibilityTypes;
import com.joysuch.wwyt.bp.service.BpExamStandardService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 考核评审管理——考核标准
 */
@RestController
@Api(tags = "核评审管理—考核标准")
@RequestMapping("/wwyt/bp-exam-standard")
public class BpExamStandardController {

    private static final Logger logger = LoggerFactory.getLogger(BpExamStandardController.class);

    @Autowired
    private BpExamStandardService bpExamStandardService;

    /**
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @RequestMapping("add")
    @RequiresPermissions("wwyt:bp-exam-standard:add")
    public Object add(@Valid BpExamStandardEditBean entity) throws Exception {
        bpExamStandardService.add(entity);
        return ResultBean.success("");
    }

    @RequestMapping("update")
    @RequiresPermissions("wwyt:bp-exam-standard:update")
    public Object update(@Valid BpExamStandardEditBean entity) throws Exception {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        bpExamStandardService.update(entity);
        return ResultBean.success("");
    }

    @RequestMapping("page")
    public Object page(Pageable page, Search condition) {
        try {
            Page<BpExamStandardListBean> data = bpExamStandardService.findByPage(page, condition);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @RequestMapping("delete")
    @RequiresPermissions("wwyt:bp-exam-standard:delete")
    public Object delete(Long id) {
        try {
            bpExamStandardService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        try {
            return ResultBean.success(bpExamStandardService.getById(id));
        } catch (Exception e) {
            logger.error("查询详情失败", e);
            return ResultBean.fail(101, "查询详情失败:" + e.getMessage());
        }
    }

    /**
     * 提交审核
     * 
     * @param id
     *            指定考核标准 ID
     */
    @RequestMapping("submitAudit")
    @RequiresPermissions("wwyt:bp-exam-standard:submitAudit")
    public Object submitAudit(Long id) {
        try {
            BpExamStandard bpExamStandard = bpExamStandardService.findById(id);
            if (bpExamStandard.getStatus() != TargetResponsibilityTypes.TO_BE_AUDITED.getId()
                    && bpExamStandard.getStatus() != TargetResponsibilityTypes.AUDIT_FAILED.getId()) {
                return ResultBean.fail(102, "状态错误");
            }
            bpExamStandardService.changeStateByAudit(id, null, null);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("提交审核失败", e);
            return ResultBean.fail(101, "提交审核失败");
        }
    }

    /**
     * 审核[通过/不通过]
     * 
     * @param id
     *            指定审核考核标准 ID
     * @param isPass
     *            是否通过（true:通过；false:不通过）
     * @param auditRemark
     *            审核意见
     */
    @RequestMapping("audit")
    @RequiresPermissions("wwyt:bp-exam-standard:audit")
    public Object audit(Long id, Boolean isPass, String auditRemark) {
        try {
            BpExamStandard bpExamStandard = bpExamStandardService.findById(id);
            if (bpExamStandard.getStatus() != TargetResponsibilityTypes.AUDIT_WAIT.getId()) {
                return ResultBean.fail(102, "状态错误");
            }
            bpExamStandardService.changeStateByAudit(id, isPass, auditRemark);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("审核操作失败", e);
            return ResultBean.fail(101, "审核操作失败");
        }
    }

}
